package application.presenter;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import javax.swing.JPanel;

import application.Node;

/** @author Krystian */

/** Klasa wizualizuje graf */

public class VisualGraph extends JPanel {

	private static final long serialVersionUID = -1520385573567591587L;

	private ArrayList<VisualNode> visualNodes = new ArrayList<VisualNode>();
	private ArrayList<VisualEdge> visualEdges = new ArrayList<VisualEdge>();
	private ArrayList<String> colors = new ArrayList<String>();

	public static int centerX, centerY;
	private int r, vR;

	private String fileColors;

	/** Konstruktor */
	public VisualGraph(Node[] nodes, int[] intColors,
			ArrayList<ArrayList<Integer>> possibleColorsNormalized,
			String fileColors) {

		this.fileColors = fileColors;

		this.setBackground(Color.white);
		loadColorsFromFile();

		int quantity = nodes.length - 1;

		r = 100 + 5 * quantity;

		centerX = 80 + r;
		centerY = 80 + r;

		this.setPreferredSize(new Dimension(160 + 2 * r, 160 + 2 * r));
		this.setSize(new Dimension(160 + 2 * r, 160 + 2 * r));

		// Ustawienie rozmiarów wierzchoków
		VisualNode.setR(20 - quantity / 10);

		for (int i = 1; i < nodes.length; i++) {

			double alpha = i * 2 * Math.PI / quantity;
			double x = Math.sin(alpha) * r + centerX;
			double y = Math.cos(alpha) * r + centerY;

			ArrayList<String> possibleColors = new ArrayList<String>();
			for (int color : possibleColorsNormalized.get(i - 1)) {
				possibleColors.add(colors.get(color));
			}

			visualNodes.add(new VisualNode(i, x, y, colors.get(intColors[i]),
					alpha, possibleColors));

		}

		for (int i = 1; i < nodes.length; i++) {
			for (Integer j : nodes[i].getAdjacentNodes()) {
				visualEdges.add(new VisualEdge(visualNodes.get(i - 1).getX(),
						visualNodes.get(i - 1).getY(), visualNodes.get(j - 1)
								.getX(), visualNodes.get(j - 1).getY()));
			}
		}

		repaint();

	}

	/** Wczytuje wartosci z pliku */
	private void loadColorsFromFile() {

		int counter = 0;
		colors.clear();

		Collections.shuffle(Arrays.asList(defaultColors));

		try {

			FileInputStream fstream = new FileInputStream(fileColors);
			System.out.println("Ładowanie nazw kolorów z :" + fileColors);
			DataInputStream in = new DataInputStream(fstream);
			@SuppressWarnings("resource")
			BufferedReader br = new BufferedReader(new InputStreamReader(in));

			String colorCode;
			while ((colorCode = br.readLine()) != null) {

				try {
					Color.decode("#" + colorCode);
					colors.add(colorCode);
				} catch (Exception e) {
					throw e;
				}

				counter++;
			}
			in.close();

		} catch (Exception e) {

			System.err.println("Error: "
					+ " Nie udao sie zaladowac nazw kolorow");
			System.err.println("Error: " + e.getMessage());

		} finally {

			int j = 0;

			for (int i = defaultColors.length; i > counter; i--) {
				colors.add(defaultColors[j++]);
			}

		}

	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);

		Graphics2D g2 = (Graphics2D) g;

		for (VisualNode node : visualNodes) {
			node.draw(g2);
		}

		for (VisualEdge edge : visualEdges) {
			edge.draw(g2);
		}

	}

	public int getvR() {
		return vR;
	}

	public void setvR(int vR) {
		this.vR = vR;
	}

	// domyslne kolory
	private String[] defaultColors = { "000000", "150517", "250517", "2B1B17",
			"302217", "302226", "342826", "34282C", "382D2C", "3B3131",
			"3E3535", "413839", "41383C", "463E3F", "4A4344", "4C4646",
			"4E4848", "504A4B", "544E4F", "565051", "595454", "5C5858",
			"5F5A59", "625D5D", "646060", "666362", "696565", "6D6968",
			"6E6A6B", "726E6D", "747170", "736F6E", "616D7E", "657383",
			"646D7E", "6D7B8D", "4C787E", "4C7D7E", "806D7E", "5E5A80",
			"4E387E", "151B54", "2B3856", "25383C", "463E41", "151B8D",
			"15317E", "342D7E", "2B60DE", "306EFF", "2B65EC", "2554C7",
			"3BB9FF", "38ACEC", "357EC7", "3090C7", "25587E", "1589FF",
			"157DEC", "1569C7", "153E7E", "2B547E", "4863A0", "6960EC",
			"8D38C9", "7A5DC7", "8467D7", "9172EC", "9E7BFF", "728FCE",
			"488AC7", "56A5EC", "5CB3FF", "659EC7", "41627E", "737CA1",
			"737CA1", "98AFC7", "F6358A", "F6358A", "E4317F", "F52887",
			"E4287C", "C12267", "7D053F", "CA226B", "C12869", "800517",
			"7D0541", "7D0552", "810541", "C12283", "E3319D", "F535AA",
			"FF00FF", "F433FF", "E238EC", "C031C7", "B048B5", "D462FF",
			"C45AEC", "A74AC7", "6A287E", "8E35EF", "893BFF", "7F38EC",
			"6C2DC7", "461B7E", "571B7E", "7D1B7E", "842DCE", "8B31C7",
			"A23BEC", "B041FF", "7E587E", "D16587", "F778A1", "E56E94",
			"C25A7C", "7E354D", "B93B8F", "F9B7FF", "E6A9EC", "C38EC7",
			"D2B9D3", "C6AEC7", "EBDDE2", "C8BBBE", "E9CFEC", "FCDFFF",
			"E3E4FA", "FDEEF4", "F0F8FF", "F0FFFF", "C6DEFF", "ADDFFF",
			"BDEDFF", "E0FFFF", "C2DFFF", "B4CFEC", "B7CEEC", "52F3FF",
			"00FFFF", "57FEFF", "50EBEC", "4EE2EC", "48CCCD", "43C6DB",
			"9AFEFF", "8EEBEC", "78c7c7", "46C7C7", "43BFC7", "77BFC7",
			"92C7C7", "AFDCEC", "3B9C9C", "307D7E", "3EA99F", "82CAFA",
			"A0CFEC", "87AFC7", "82CAFF", "79BAEC", "566D7E", "6698FF",
			"736AFF", "CFECEC", "AFC7C7", "717D7D", "95B9C7", "5E767E",
			"5E7D7E", "617C58", "7FFFD4", "348781", "306754", "4E8975",
			"254117", "387C44", "4E9258", "347235", "347C2C", "667C26",
			"437C17", "347C17", "348017", "4AA02C", "41A317", "4AA02C",
			"8BB381", "99C68E", "4CC417", "6CC417", "52D017", "4CC552",
			"54C571", "57E964", "5EFB6E", "64E986", "6AFB92", "B5EAAA",
			"C3FDB8", "00FF00", "87F717", "5FFB17", "59E817", "7FE817",
			"8AFB17", "B1FB17", "CCFB5D", "BCE954", "A0C544", "FFFF00",
			"FFFC17", "FFF380", "EDE275", "EDDA74", "EAC117", "FDD017",
			"FBB917", "E9AB17", "D4A017", "C7A317", "C68E17", "AF7817",
			"ADA96E", "C9BE62", "827839", "FBB117", "E8A317", "C58917",
			"F87431", "E66C2C", "F88017", "F87217", "E56717", "C35617",
			"C35817", "8A4117", "7E3517", "7E2217", "7E3117", "7E3817",
			"7F5217", "806517", "805817", "7F462C", "C85A17", "C34A2C",
			"E55B3C", "F76541", "E18B6B", "F88158", "E67451", "C36241",
			"C47451", "E78A61", "F9966B", "EE9A4D", "F660AB", "F665AB",
			"E45E9D", "C25283", "7D2252", "E77471", "F75D59", "E55451",
			"C24641", "FF0000", "F62217", "E41B17", "F62817", "E42217",
			"C11B17", "FAAFBE", "FBBBB9", "E8ADAA", "E7A1B0", "FAAFBA",
			"F9A7B0", "E799A3", "C48793", "C5908E", "B38481", "C48189",
			"7F5A58", "7F4E52", "7F525D", "817679", "817339", "827B60",
			"C9C299", "C8B560", "ECD672", "ECD872", "FFE87C", "ECE5B6",
			"FFF8C6", "FAF8CC", "F5F5DC", "FAEBD7" };

}